package com.jy.wms.service;

import com.jy.wms.common.base.BaseService;
import com.jy.wms.pojo.WmsStoreHouse;
import com.jy.wms.dao.WmsStoreHouseMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
    * @ClassName: WmsStoreHouseService.java
    * @Description: TODO(这里用一句话描述这个类的作用)
    * @Author lqc
    * @Date Thu Jun 28 14:23:32 CST 2018
    */

    @Service
    public class WmsStoreHouseService extends BaseService<WmsStoreHouseMapper,WmsStoreHouse> {

    private static Logger log = LoggerFactory.getLogger(WmsStoreHouseService.class);

    /**
    * 期初期末查询
    * @param entity 实体
    * @return Page<WmsStoreHouse>
    */
    public Page<WmsStoreHouse> baseSelectPage(WmsStoreHouse entity ){
        PageHelper.startPage(entity.getPage(),entity.getRows());
        return (Page<WmsStoreHouse>)dao.baseSelectPage( entity );
    }

   /**
    * 定时器
    * @return WmsStoreHouse
    */
    public void baseSelectByPrimaryKey() throws ParseException {

        //获取当前系统时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        //为获取当前系统时间
        String sj = df.format(System.currentTimeMillis());
        log.info("今天的前系统时间"+sj);
        //String sj="2018-07-16";
        //查询拣货数量
        List<WmsStoreHouse> sjsl=dao.numbnerofqueriesontheshelf(sj);
        for (WmsStoreHouse sll:sjsl){
            String hpid=sll.getHpid();
            int sl=sll.getSjsl();
            log.info("查询拣货数量"+sl);

        //查询上架数量
         int jhsl=dao.Numberofinquiries(sj,hpid);//0
        log.info("查询上架数量"+jhsl);
         int a =0;
        //计算当天数量(上架数量减去拣货数量)
        int mtkc=jhsl-sl;
        log.info("上架数量减去拣货数量"+mtkc);
        //获取上一天时间
         String date=WmsStoreHouseService.dateform();
         log.info("昨天时间:"+date);
        //String date="2018-07-15";
        //查询上一天的数量
        int zqsl=dao.Previousnumberofqueries(hpid,date);
        log.info("查询上一天的数量"+zqsl);
        //总期初库存
        int qckc= mtkc+zqsl;
        log.info(qckc+"总期初库存");
        try {
            //保存期初库存
            if (hpid == null) {
                hpid = "";
                log.info("查询到货品id为空"+hpid);
                dao.baseInsert(hpid, qckc, sj, mtkc);
            } else {
                dao.baseInsert(hpid, qckc, sj, mtkc);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            }
        }
    }

    //获取上一天时间
    public static String dateform() throws ParseException {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = new Date();
        Calendar date = Calendar.getInstance();
        date.setTime(beginDate);
        date.set(Calendar.DATE, date.get(Calendar.DATE) - 1);
        Date endDate = dft.parse(dft.format(date.getTime()));
        String formatDate = DateFormat.getDateInstance().format(endDate);
        System.out.println(formatDate);
        return formatDate;
    }
}
